package com.mm.controller.web;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.stream.CollectorUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.common.DCommonCode;
import com.mm.serivce.common.IDCommonCodeService;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import com.mm.vo.web.user.DownTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 描述: 公共配置
 *
 * @author: yanglin
 * @Date: 2021-08-31-9:34
 * @Version: 1.0
 */
@Api(value = "/common/v1/", tags = {"描述: 公共配置"})
@Slf4j
@RestController
@RequestMapping("/common/v1/")
public class CommonCodeController {

    private final IDCommonCodeService commonCodeService;

    public CommonCodeController(IDCommonCodeService commonCodeService) {
        this.commonCodeService = commonCodeService;
    }

    /**
     * 公共配置分页
     *
     * @param page
     * @param limit
     * @param commonCode
     * @param commonText
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "page", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "limit", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "commonCode", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "commonText", value = "")
    })
    @ApiOperation(value = "公共配置分页", notes = "公共配置分页", httpMethod = "POST")
    @PostMapping("/queryCommonCodePage")
    public DResult queryCommonCodePage(@RequestParam int page, @RequestParam int limit, @RequestParam(required = false) Integer commonCode, @RequestParam(required = false) String commonText) {
        Page<DCommonCode> commonCodePage = commonCodeService.queryCommonCodePage(page, limit, commonCode, commonText);
        DPage<DCommonCode> commonCodeDPage = DPage.to(commonCodePage);
        return DResult.of(commonCodeDPage);
    }

    /**
     * 公共配置列表
     *
     * @param code
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "code", value = "", required = true)
    })
    @ApiOperation(value = "公共配置列表", notes = "公共配置列表", httpMethod = "GET")
    @GetMapping("/queryCommonCodeList")
    public DResult queryCommonCodeList(@RequestParam Integer code) {
        return DResult.of(commonCodeService.queryCommonCodeList(code));
    }

    /**
     * 公共配置明细
     *
     * @param commonId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "commonId", value = "", required = true)
    })
    @ApiOperation(value = "公共配置明细", notes = "公共配置明细", httpMethod = "POST")
    @PostMapping("/queryCommonCodeOne")
    public DResult queryCommonCodeOne(@RequestParam Long commonId) {
        return DResult.of(commonCodeService.queryCommonCodeOne(commonId));
    }

    /**
     * 公共配置编辑
     *
     * @param commonCode
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DCommonCode", dataTypeClass = DCommonCode.class, name = "commonCode", value = "", required = true)
    })
    @ApiOperation(value = "公共配置编辑", notes = "公共配置编辑", httpMethod = "POST")
    @Transactional
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DCommonCode commonCode) {
        boolean flag = false;
        if (commonCode.getId() < 0) {
            // 新增
            flag = commonCodeService.save(commonCode);
        } else if (commonCode.getId() > 0) {
            // 更新
            flag = commonCodeService.updateById(commonCode);
        }
        return DResult.of(flag);
    }

    /**
     * 公共配置删除
     *
     * @param commonId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "commonId", value = "", required = true)
    })
    @ApiOperation(value = "公共配置删除", notes = "公共配置删除", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Long commonId) {
        boolean flag = false;
        if (commonId > 0) {
            flag = commonCodeService.removeCommonCode(commonId);
        }
        return DResult.of(flag);
    }

    /**
     * 公共配置-单个code
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "公共配置-单个code", notes = "公共配置-单个code", httpMethod = "GET")
    @GetMapping("/queryCommonCode")
    public DResult queryCommonCode(Integer code) {
        List<DCommonCode> commonCodeList = commonCodeService.queryCommonCodeList(code);
        List<DownTreeVo> downTreeVos = new ArrayList<>();
        Optional.ofNullable(commonCodeList).orElse(new ArrayList<>())
                .forEach(a -> {
                    downTreeVos.add(DownTreeVo.builder().id(Integer.valueOf(a.getName()))
                            .title(a.getContent()).build());
                });
        return DResult.of(downTreeVos);
    }

    /**
     * 公共配置-多个code
     *
     * @param codes
     * @return
     */
    @ApiOperation(value = "公共配置-多个code", notes = "公共配置-多个code", httpMethod = "GET")
    @GetMapping("/queryCommonManyCode")
    public DResult queryCommonManyCode(String codes) {
        List<String> codeStr = StrUtil.split(codes, ",");
        List<Integer> codeList = codeStr.stream().map(code -> Integer.valueOf(code)).collect(Collectors.toList());
        List<DCommonCode> commonCodeList = commonCodeService.queryCommonCodeList(codeList);
        if (CollectionUtil.isEmpty(commonCodeList)) {
            return DResult.failed();
        }
        List<DownTreeVo> sourceDownVos = new ArrayList<>();
        Optional.ofNullable(commonCodeList).orElse(new ArrayList<>())
                .forEach(c -> {
                    sourceDownVos.add(DownTreeVo.builder().id(Integer.valueOf(c.getName()))
                            .code(c.getCode()).title(c.getContent()).build());
                });
        Map<Integer, List<DownTreeVo>> downListMap = sourceDownVos.stream().collect(Collectors.groupingBy(DownTreeVo::getCode));
        return DResult.of(downListMap);
    }

}
